package com.sky.myapplication.kotlin.kotlinActivity

import android.content.Intent
import android.os.Bundle
import android.os.storage.StorageManager
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.sky.myapplication.R
import com.sky.myapplication.java.data.FileEntity
import com.sky.myapplication.java.utils.FileOpenUtils
import com.sky.myapplication.kotlin.kotlinAdapter.FileAdapter
import java.io.File
import java.util.*
import kotlin.Comparator
import kotlin.collections.ArrayList

/**
 * Created by Min
 * Date 2023/2/2  15:42
 */
class FileSecondManagerKotlinActivity : AppCompatActivity() {
    lateinit var mStorageManager : StorageManager
    lateinit var rv_file_list : RecyclerView
    lateinit var adapter : FileAdapter
    var dataList = ArrayList<FileEntity>()

    lateinit var path : String

    override fun onCreate(bundle: Bundle?) {
        super.onCreate(bundle)
        setContentView(R.layout.activity_file_manager)

        if (intent.getStringExtra("path") != null){
            path = intent.getStringExtra("path")!!
        } else {
            Log.i("mm", "no path")
        }

        rv_file_list = findViewById(R.id.rv_file_list)
        rv_file_list.layoutManager = LinearLayoutManager(this)
        adapter = FileAdapter(this, dataList)
        adapter.setFileAdapterClick(object : FileAdapter.FileAdapterClick{
            override fun itemClick(entity: FileEntity) {
                if (entity.directory){
                    val intent = Intent(this@FileSecondManagerKotlinActivity, FileSecondManagerKotlinActivity::class.java)
                    intent.putExtra("path", entity.path)
                    startActivity(intent)
                } else {
                    FileOpenUtils.openFile(this@FileSecondManagerKotlinActivity, entity.path)
                }
            }
        })
        rv_file_list.adapter = adapter

        getFileList(path)
    }

    fun getFileList(path : String){
        try {
            var file = File(path)
            var fileList = file.listFiles()

            var directoryList = ArrayList<FileEntity>()
            var sourceList = ArrayList<FileEntity>()

            for (f in fileList){
                var file = FileEntity(f.name, f.path, f.isDirectory)
                if (f.isDirectory){
                    directoryList.add(file)
                } else{
                    sourceList.add(file)
                }
            }

            var comparator = object : Comparator<FileEntity>{
                override fun compare(p0: FileEntity?, p1: FileEntity?): Int {
                    return p0!!.name!!.compareTo(p1!!.name)!!
                }
            }
            Collections.sort(directoryList, comparator)
            Collections.sort(sourceList, comparator)

            dataList.addAll(directoryList)
            dataList.addAll(sourceList)
        } catch (e : java.lang.Exception){

        }
//                dataList.addAll(list.sortedWith(compareBy({it.name})) as ArrayList<FileEntity>)
        rv_file_list.adapter?.notifyDataSetChanged()
    }
}